package com.ilop.sthome.ui.activity.scene;


import android.content.Intent;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;

import com.ilop.sthome.ui.activity.main.ContainLoginActivity;
import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.greenDao.AutomationBean;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.mvp.contract.scene.ChooseActionContract;
import com.ilop.sthome.mvp.present.scene.ChooseActionPresenter;
import com.ilop.sthome.ui.adapter.scene.ChoseDeviceAdapter;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityChooseActionBinding;

import java.util.List;

/**
 * @author skygge
 * @date 2019-12-12.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：自动化添加页面
 */
public class ChooseActionActivity extends ContainLoginActivity<ChooseActionPresenter, ActivityChooseActionBinding> implements ChooseActionContract.IView {

    private ChoseDeviceAdapter mAdapter;
    private ChoseDeviceAdapter mAdapter2;
    private String mDeviceName;
    private boolean isInput;
    private boolean isAddAuto;
    private boolean isLinkage;
    private AutomationBean mAutoBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_choose_action;
    }

    @Override
    protected void initialize() {
        super.initialize();
        mDeviceName = getIntent().getStringExtra(CommonId.KEY_DEVICE_NAME);
        isAddAuto = getIntent().getBooleanExtra(CommonId.KEY_ADD_SCENE, false);
        isInput = getIntent().getBooleanExtra(CommonId.KEY_INPUT, true);
        isLinkage = getIntent().getBooleanExtra(CommonId.KEY_LINKAGE, false);
        mAutoBean = (AutomationBean) getIntent().getSerializableExtra(CommonId.KEY_AUTOMATION);
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresent = new ChooseActionPresenter(mContext, mDeviceName);
    }

    @Override
    protected void initView() {
        super.initView();
        mDBind.tvConditionTitle.setText(isInput ? getString(R.string.increase_input) : getString(R.string.increase_output));
        if (!isInput){
            mDBind.choseTitle.setText(getString(R.string.just_do_what));
        }
        if (isLinkage){
            mDBind.chooseNotLinkage.setVisibility(View.GONE);
        }
        mAdapter = new ChoseDeviceAdapter(mContext);
        mAdapter2 = new ChoseDeviceAdapter(mContext);
        mDBind.choseFictitiousList.setLayoutManager(new LinearLayoutManager(mContext));
        mDBind.choseFictitiousList.setAdapter(mAdapter);
        mDBind.choseEquipmentList.setLayoutManager(new LinearLayoutManager(mContext));
        mDBind.choseEquipmentList.setAdapter(mAdapter2);
    }

    @Override
    protected void initData() {
        super.initData();
        if (isInput){
            mPresent.getDeviceInList(mDeviceName);
        }else {
            mPresent.getDeviceOutList(mDeviceName);
        }

        LiveDataBus.get().with(CommonId.CHOOSE_CONDITION, DeviceInfoBean.class).observe(this, device ->
                mPresent.onItemClick(device, mAutoBean, isAddAuto, isInput));
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivConditionBack.setOnClickListener(v -> finish());
    }

    @Override
    public void getFictitiousList(List<DeviceInfoBean> device) {
        mAdapter.setLists(device);
    }

    @Override
    public void getDeviceList(List<DeviceInfoBean> device) {
        mAdapter2.setLists(device);
    }

    @Override
    public void withoutData() {
        mAdapter2.setLists(null);
    }

    @Override
    public void startActivityByIntent(Intent intent) {
        startActivity(intent);
        finish();
    }

    @Override
    public void finishActivity() {
        finish();
    }
}
